<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="上课日期" prop="courseData">
        <el-input
          v-model="queryParams.courseData"
          placeholder="请输入上课日期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
      </el-col>
      <el-col :span="1.5">
        <el-upload
          :show-file-list="false"
          accept="application/vnd.ms-excel"
          action="http://localhost:8282/system/course/importCourse"
          :on-success="fileUploadSuccess"
          :on-error="fileUploadError"
          :disabled="fileUploadBtnText=='正在导入'"
          :before-upload="beforeFileUpload"
          style="display: inline">
          <el-button  type="success"
                      :loading="fileUploadBtnText=='正在导入'"
                      size="mini">
            <i class="el-icon-upload el-icon--right"></i>
            {{fileUploadBtnText}}
          </el-button>
        </el-upload>

      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="courseList" @selection-change="handleSelectionChange" size='mini' style="width: 95%">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="课程名称" align="center" prop="courseName" />
      <el-table-column label="学时" align="center" prop="coursePeriod" />
      <el-table-column label="学分" align="center" prop="courseScore" />
      <el-table-column label="课程介绍" align="center" prop="courseIntroduce" />
      <el-table-column label="上课时间
" align="center" prop="courseTime" />
      <el-table-column label="上课日期" align="center" prop="courseData" />
      <el-table-column label="消息推送 " align="center" prop="status" >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right"
                       prop="px"
                       width="120"
      >
        <template slot-scope="scope">
          <el-col :span="1.5">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          </el-col>
          <el-col :span="1.5">
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          </el-col>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="课程号" prop="courseNo">
          <el-input v-model="form.courseNo" placeholder="请输入课程号" />
        </el-form-item>
        <el-form-item label="课程名称" prop="courseName">
          <el-input v-model="form.courseName" placeholder="请输入课程名称" />
        </el-form-item>
        <el-form-item label="学时" prop="coursePeriod">
          <el-input v-model="form.coursePeriod" placeholder="请输入学时" />
        </el-form-item>
        <el-form-item label="学分" prop="courseScore">
          <el-input v-model="form.courseScore" placeholder="请输入学分" />
        </el-form-item>
        <el-form-item label="课程介绍" prop="courseIntroduce">
          <el-input v-model="form.courseIntroduce" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="上课时间
" prop="courseTime">
          <el-input v-model="form.courseTime" placeholder="请输入上课时间
" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="上课日期" prop="courseData">
          <el-input v-model="form.courseData" placeholder="请输入上课日期(在第几学期开课)" />
        </el-form-item>
        <el-form-item label="定时任务是否开启 " prop="status">
          <el-input v-model="form.status" placeholder="请输入定时任务是否开启 " />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCourse, getCourse, delCourse, addCourse, updateCourse } from "@/api/evaluation/course";

export default {
  name: "Course",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程信息表格数据
      courseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseNo: null,
        courseName: null,
        coursePeriod: null,
        courseScore: null,
        courseIntroduce: null,
        courseTime: null,
        courseData: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      fileUploadBtnText:'导入',
    };
  },
  created() {
    this.getList();
  },
  methods: {
    fileUploadSuccess(response, file,fileList){
      if (response){
        //this.$message({type:response.status,message:response.msg});
        this.$alert('导入成功', '消息', {
          confirmButtonText: '确定',
        })
        this.fileUploadBtnText='导入数据'
      }
    },
    fileUploadError(err,file,fileList){
      this.$message({type:'error',message:"导入数据！"});
      this.fileUploadBtnText='导入数据';
    },
    beforeFileUpload(file){
      this.fileUploadBtnText='正在导入';
    },

    /** 查询课程信息列表 */
    getList() {
      this.loading = true;
      listCourse(this.queryParams).then(response => {
        this.courseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //定时任务状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.courseName + '"随堂消息推送吗？').then(function() {
        return updateCourse(row);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    // 表单重置
    reset() {
      this.form = {
        courseId: null,
        courseNo: null,
        courseName: null,
        coursePeriod: null,
        courseScore: null,
        courseIntroduce: null,
        courseTime: null,
        remark: null,
        updateTime: null,
        createTime: null,
        spareOne: null,
        spateTwo: null,
        createBy: null,
        courseData: null,
        cronExpression: null,
        status: null,
        invokeMethod: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.courseId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const courseId = row.courseId || this.ids
      getCourse(courseId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.courseId != null) {
            updateCourse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const courseIds = row.courseId || this.ids;
      this.$modal.confirm('是否确认删除课程信息编号为"' + courseIds + '"的数据项？').then(function() {
        return delCourse(courseIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/course/export', {
        ...this.queryParams
      }, `course_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
  .app-container {
    width: 100%;
    padding: 20px;
  }
</style>
